package com.situ.javase.day17;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.sound.sampled.LineListener;

import org.junit.Test;


public class JDBCDemo {
	

	@Test
	public void testJDBC() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		
		try {
			// 1、加载驱动Class.forName("");
			Class.forName("com.mysql.jdbc.Driver");
			// 2、获得连接对象Connection
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java1907?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
			// 3、写sql语句
			String sql = "SELECT id,`name`,age,gender FROM student";
			// 4、创建Statement
			statement = connection.createStatement();
			// 5、执行语句
			//     (1)、更新类（只要改变表里面的数据） ：delete、update、insert                    executeUpdate()
			//             返回值：int    表示影响的行数
			//     (2)、查询类（没有改变表里面的数据） ：select                                                executeQuery()
			//             返回值：结果集ResultSet
			resultSet = statement.executeQuery(sql);
			ArrayList<Student> list = new ArrayList<Student>();
			// 判断下一行有没有，如果有返回true，指向下一行，如果没有返回false
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 6、关闭连接
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testSelect() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,`name`,age,gender FROM student";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
	}
	
	@Test
	public void testDelete() {
		Connection connection = null;
		Statement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			statement = connection.createStatement();
			String sql = "delete from student where id=5";
			int count = statement.executeUpdate(sql);
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
	}
	
	
	
	@Test
	public void testPreparedStatementSelect() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,`name`,age,gender FROM student";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
	}
	
	
	@Test
	public void testPreparedStatementSelect1() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		String stuGender= "男";
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,`name`,age,gender FROM student where gender=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, stuGender);
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			ArrayList<Student> list = new ArrayList<Student>();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
	}
	
	@Test
	public void testPreparedStatementInsert() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO student(`name`,age,gender) VALUES(?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "赵六");
			preparedStatement.setInt(2, 23);
			preparedStatement.setString(3, "女");
			System.out.println(preparedStatement);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
	}	
	
	@Test
	public void testPreparedStatementDelete() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, 3);
			System.out.println(preparedStatement);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
	}
	
	@Test
	public void testPreparedStatementUpdate() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "update student set name=?,age=?,gender=? where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "小李");
			preparedStatement.setInt(2, 23);
			preparedStatement.setString(3, "女");
			preparedStatement.setInt(4, 2);
			System.out.println(preparedStatement);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("更新成功");
			} else {
				System.out.println("更新失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
	}
	
}
